package com.heima.threadPool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class ThreadTest {

    /**
     * 主方法
     * @param args
     */
    public static void main(String[] args) {

        //继承Thread
        myThread myThread1=new myThread();
        myThread1.setName("thread1_");
        myThread1.start();//启动线程

        myThread myThread2=new myThread();
        myThread2.setName("thread2_");
        myThread2.start();//启动线程

        //实现runnable接口，创建一个线程类即可，多个线程之间可以共享
        myRunnable myRunnable=new myRunnable();

        Thread thread1=new Thread(myRunnable);
        thread1.setName("runnable1_");
        thread1.start();//启动线程

        Thread thread2=new Thread(myRunnable);
        thread2.setName("runnable2_");
        thread2.start();//启动线程


        MyCallable myCallable=new MyCallable();
        // 创建F
        FutureTask<String> ft = new FutureTask<String>(myCallable) ;

        // 创建Thread对象
        Thread t1 = new Thread(ft) ;
        t1.setName("t1Name_");
        Thread t2 = new Thread(ft) ;
        t2.setName("t2Name_");

        t1.start();
        t2.start();

        String result = null;
        try {
            result = ft.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(result);







        //打印主线程
        System.out.println(Thread.currentThread().getName()+"_"+System.currentTimeMillis());
    }

    /**
     * 创建Runnable
     *
     */
   public static class myRunnable implements Runnable{

        @Override
        public void run() {
         System.out.println(Thread.currentThread().getName()+System.currentTimeMillis());
        }
    }

    /**
     * 创建thread
     */
    public static class myThread extends Thread{
        @Override
        public void run() {
         System.out.println(Thread.currentThread().getName()+System.currentTimeMillis());
        }
    }

    public static class MyCallable implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println(Thread.currentThread().getName() + System.currentTimeMillis());
            return "OK";
        }
    }
}
